package zy.data.food.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zy.data.food.dto.OrderDTO;
import zy.data.food.enums.ResultEnum;
import zy.data.food.exception.SellException;
import zy.data.food.service.BuyerService;
import zy.data.food.service.OrderService;

/**
 * @Auther: zhangy
 * @Date: 2018/9/10 10:32
 * @Description: 买家单个订单查询和取消的接口实现
 */
@Service
@Slf4j
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private OrderService orderService;

    @Override
    public OrderDTO findOrderOne(String openid, String orderId) {
        return checkOrder(openid,orderId);
    }

    @Override
    public OrderDTO cancleOrder(String openid, String orderId) {
        OrderDTO orderDTO = orderService.findone(orderId);
        if (orderDTO == null){
            log.error("【取消订单】查不到该订单，orderId={}",orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);

        }
        return orderService.cancle(orderDTO);
    }


    //openid和orderId验证方法
    public OrderDTO checkOrder(String openid,String orderId){
        OrderDTO orderDTO = orderService.findone(orderId);
        if(orderDTO == null){
                return null;
        }

        if(!orderDTO.getBuyerOpenid().equals(openid)){
            log.error("【订单查询】订单的openid不一致, openid={},orderDTO={}",openid,orderDTO);
            throw new SellException(ResultEnum.ORDER_OWNER_ERROR);
        }

        return orderDTO;
    }
}
